<?php
/**
 * Created by PhpStorm.
 * User: alexey
 * Date: 5/18/14
 * Time: 1:57 PM
 */

namespace controller;

include_once("Parcer.php");
include_once("Mapping.php");

class ActionParcer implements Parcer
{

  static function parse($parseArray)
  {
    $splMap = new \SplObjectStorage();
    $arrayLength = count($parseArray);
    for ($i = 0; $i < $arrayLength; $i++) {
      $mapping = new Mapping();
      $action = ActionParcer::getAction($parseArray[$i]);
      if ($action) {
        $mapping->setAction($action);
        $mapping->setFrom(ActionParcer::getFrom($parseArray[$i]));
        $splMap->attach($mapping);
      }
    }
    return $splMap;
  }

  private static function getAction($parseString)
  {
    $actionStartPos = stripos($parseString, "apping");
    if (!$actionStartPos) {
      return false;
    }
    $actionStartPos = $actionStartPos + 7;
    $actionEndPos = stripos($parseString, "=", $actionStartPos);
    $action = trim(substr($parseString, $actionStartPos, $actionEndPos - $actionStartPos)) . ".php";

    return $action;
  }

  private static function getFrom($parseString)
  {
    $actionStartPos = stripos($parseString, "apping");
    if (!$actionStartPos) {
      return false;
    }
    $actionStartPos = stripos($parseString, "=") + 1;
    $action = trim(substr($parseString, $actionStartPos));

    return $action;
  }
}